package com.example.demo.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.demo.entity.Soft;
import com.example.demo.service.SoftService;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;


import static com.example.demo.entity.table.SoftTableDef.SOFT;

/**
 *  控制层。
 *
 * @author zql
 * @since 2023-11-05
 */
@Slf4j
@RestController
@RequestMapping("/soft")
public class SoftController {

    @Autowired
    private SoftService softService;

    /**
     * 添加。
     *
     * @param soft 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody Soft soft) {
        return softService.save(soft);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return softService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param soft 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody Soft soft) {
        return softService.updateById(soft);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<Soft> list() {
        return softService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public Soft getInfo(@PathVariable Serializable id) {
        return softService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Soft> page(Page<Soft> page) {
        return softService.page(page);
    }

    @GetMapping("list/pageNumber={pageNumber}&pageSize={pageSize}")
    public List<Soft> listSoft(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize) {
        // paginate(pageNumber, pageSize, totalRow, queryWrapper)：分页查询。
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SOFT.ALL_COLUMNS)
                .limit((pageNumber-1) * pageSize, pageSize);

        return softService.list(queryWrapper);
    }

//    @GetMapping("list/pageNumber={pageNumber}&pageSize={pageSize}")
//    public Page<Soft> list(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize) {
//        // paginate(pageNumber, pageSize, totalRow, queryWrapper)：分页查询。
//        QueryWrapper queryWrapper = QueryWrapper.create()
//                .select(SOFT.ALL_COLUMNS)
//                .limit((pageNumber-1) * pageSize, pageSize);
//        softService.page
//        return softService.page(queryWrapper.ge(1));
//    }

    /*
    分页模糊查询
    * */
    @GetMapping("/pageDef")
    public Page<Soft> pageDef(
            // @RequestParam("softName") String softName,
            @RequestParam(defaultValue = "") String softName,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "6") Integer size
    ){
        return softService.pageDef(current, size, softName);
    }

    /*
    * Integer current, Integer size, Integer softId,
      String softName, String softVersion,
      String gitUrl, String officialUrl, String mainLanguage,
      String languages, String startTime, String endTime

      * @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "20") Integer pageSize,
            @RequestParam(defaultValue = "") String softId,
            @RequestParam(defaultValue = "") String softName,
            @RequestParam(defaultValue = "") String softVersion,
            @RequestParam(defaultValue = "") String gitUrl,
            @RequestParam(defaultValue = "") String officialUrl,
            @RequestParam(defaultValue = "") String mainLanguage,
            @RequestParam(defaultValue = "") String languages,
            @RequestParam(defaultValue = "") String startTime,
            @RequestParam(defaultValue = "") String endTime

           这个是get用的！！！
    * */
    @PostMapping("queryFuzzy") // 错误，可以先用@GetMapping 试试 PostMapping
    public Page<Soft> postQueryFuzzy(@RequestBody HashMap map) {
        Integer current = map.get("current") != null ?  (Integer) map.get("current") : 1;
        Integer pageSize = map.get("pageSize") != null ? (Integer) map.get("pageSize") : 20;
        String softId = map.get("softId") != null ? (String) map.get("softId") : "";
        String softName = map.get("softName") != null ? (String) map.get("softName") : "";
        String softVersion = map.get("softVersion") != null ? (String) map.get("softVersion") : "";
        String gitUrl = map.get("gitUrl") != null ? (String) map.get("gitUrl") : "";
        String officialUrl = map.get("officialUrl") != null ? (String) map.get("officialUrl") : "";
        String mainLanguage = map.get("mainLanguage") != null ? (String) map.get("mainLanguage") : "";
        String languages = map.get("languages") != null ? (String) map.get("languages") : "";
        String startTime = map.get("startTime") != null ? (String) map.get("startTime") : "";
        String endTime = map.get("endTime") != null ? (String) map.get("endTime") : "";

        log.info("current,  pageSize, softId,\n" +
                "softName, softVersion, gitUrl, officialUrl,\n" +
                "mainLanguage, languages, startTime, endTime::::::: " +
                "{}, {}, {}, {} {} {} {} {} {} {} {}", current,  pageSize, softId,
                softName, softVersion, gitUrl, officialUrl,
                mainLanguage, languages, startTime, endTime);

        return softService.queryFuzzy(current,  pageSize, softId,
                softName, softVersion, gitUrl, officialUrl,
                mainLanguage, languages, startTime, endTime
        );
        // 掉不了，有误？？
    }

    @PostMapping("/saveDef")
    public boolean saveDef(@RequestBody Soft soft) {
        return softService.saveOrUpdateByIdsDef(soft);
    }

    @PostMapping("/deleteDef")
    public boolean deleteDef(@RequestBody List<Integer> ids) {
        return softService.deleteBatchIdsDef(ids);
    }

}
